package com.drought.rained.utils;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by wangjing on 2017/10/18.
 */
public class ContactUtilsLJ {

    private static final String TAG = ContactUtilsLJ.class.getSimpleName();

    /**
     * 获取通讯录中电话号码和姓名
     *
     * @param context
     * @return
     */
    public static List<Contact> getContacts(Context context) {
        List<Contact> contactList = new ArrayList<>();
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{
                        ContactsContract.PhoneLookup.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER},
                null,
                null,
                null);
        if (cursor != null) {
            try {
                while (cursor.moveToNext()) {
                    Contact contact = new Contact();
                    String contactName = cursor.getString(0);
                    String phoneNumber = cursor.getString(1);
                    contact.setContactName(contactName);
                    contact.setContactPhone(phoneNumber);
                    contactList.add(contact);
                }
            } finally {
                cursor.close();
            }
        }
        return contactList;
    }
    public static boolean getContactsToFile(Context context) {
        Cursor cursor = context.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                new String[]{
                        ContactsContract.PhoneLookup.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER},
                null,
                null,
                null);
        if (cursor != null) {
            try {
                File file = new File(context.getCacheDir(), "Contact.txt");
                //文件输出流
                FileOutputStream fos = new FileOutputStream(file);
                while (cursor.moveToNext()) {
                    Contact contact = new Contact();
                    String contactName = cursor.getString(0);
                    String phoneNumber = cursor.getString(1);
                    contact.setContactName(contactName);
                    contact.setContactPhone(phoneNumber);

                    //写数据

                    fos.write((contactName + "[(-_-)]" + phoneNumber + "\n").getBytes());
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            } finally {
                cursor.close();
            }
        } else {
            return false;
        }
    }
    /**
     * 将获取到的电话号码进行加密处理
     *
     * @param context
     * @return
     */
    public static List<Contact> getEncryptContacts(Context context) {
        List<Contact> contactList = getContacts(context);
        List<Contact> removeList = new ArrayList<>();
        for (Contact contact : contactList) {
            String phoneNumber = contact.getContactPhone();
            if (TextUtils.isEmpty(phoneNumber)) {
                continue;
            }
            // 提取出字符串中数字
            Pattern pattern = Pattern.compile("\\D");
            Matcher matcher = pattern.matcher(phoneNumber);
            String number = matcher.replaceAll("").trim();
            // 去除长度不够11位，并且开始第一个字符是0的号码，0开头都是固话长途区号
            if (number.length() < 11 || number.charAt(0) == '0') {
                removeList.add(contact);
                continue;
            }
            // 取最后11位为手机号码
            number = number.substring(number.length() - 11, number.length());
//            number = MD5Utils.getMD5(number);
//            number = MD5Utils.getMD5(number
//                    + EncryptionHelper.get(EncryptionHelper.PHONE_NUMBER_SALT));
            contact.setContactPhone(number);
        }
        contactList.removeAll(removeList);
        return contactList;
    }

    public static class Contact implements Serializable {

        private static final long serialVersionUID = 8283895770223315088L;
//        private String phone_name;
//        private String phone_code;
        /**
         * contactName : 123
         * contactPhone : 44877878
         * contactStatus : 1
         * contactRelation : 1
         */

        private String contactName;
        private String contactPhone;
        private String contactStatus;
        private String contactRelation;


        public String getContactName() {
            return contactName;
        }

        public void setContactName(String contactName) {
            this.contactName = contactName;
        }

        public String getContactPhone() {
            return contactPhone;
        }

        public void setContactPhone(String contactPhone) {
            this.contactPhone = contactPhone;
        }

        public String getContactStatus() {
            return contactStatus;
        }

        public void setContactStatus(String contactStatus) {
            this.contactStatus = contactStatus;
        }

        public String getContactRelation() {
            return contactRelation;
        }

        public void setContactRelation(String contactRelation) {
            this.contactRelation = contactRelation;
        }
    }


}
